package com.smartEdu.service.impl.app;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.smartEdu.RongYun.RongYun1;
import com.smartEdu.commons.CMap;
import com.smartEdu.commons.LogCvt;
import com.smartEdu.commons.Session;
import com.smartEdu.commons.Tag_Tips;
import com.smartEdu.dao.FindFriendDao;
import com.smartEdu.dao.GroupDao;
import com.smartEdu.dao.RongCloudDao;
import com.smartEdu.dao.RongCloudDao2;
import com.smartEdu.exception.JsonException;
import com.smartEdu.exception.ServiceException;
import com.smartEdu.model.CreateRongGroupModel;
import com.smartEdu.model.FindFriendModel;
import com.smartEdu.model.GroupIdModel;
import com.smartEdu.model.RongCloudModel;
import com.smartEdu.service.RongCloudService2;
@Service(value="rongCloudeServiceImpl2")
public class RongCloudServiceImpl2 extends BaseServiceImpl implements RongCloudService2{
 
	@Autowired
	@Qualifier(value="rongCloudDaoImpl2")
	public RongCloudDao2 rongCloudeDao2;
	

	@Override
	public void returnGroupNumber(Session sess) {
		// TODO Auto-generated method stub
		CMap map=null;
		try{
			map=SetToMap(sess);
			List<FindFriendModel> model=new ArrayList<FindFriendModel>();
			model=rongCloudeDao2.returnGroupNumber(map.gets("groupid"));
			CreateRongGroupModel model1=new CreateRongGroupModel();
			model1=rongCloudeDao2.returnGroupNumber2(map.gets("groupid"));
			sess.put("id",model1.getUserid());
			sess.put("groupname", model1.getName());
			sess.put(Tag_Tips.RESPCODE, "0");
			sess.put("groupnumberinfo", model);
		}catch (JsonException e){
			LogCvt.error(Tag_Tips.JSONERROR,e);
			throw new ServiceException("rongtoken业务流程出错");
		}
		catch (Exception e) {
			LogCvt.error(e.getMessage(),e);
			throw new ServiceException("rongtoken业务流程出错");
		}
		finally {
			map=null;
		}
		
	}


	@Override
	public void deleteGroupOne(Session sess) {
		// TODO Auto-generated method stub
		CMap map=null;
		try{
			map=SetToMap(sess);
			GroupIdModel model=new GroupIdModel();
			RongYun1 rongyun=new RongYun1();
			String url="http://api.cn.ronghub.com/group/quit.json";
			String url1="http://api.cn.ronghub.com/group/dismiss.json";
			if(map.gets("type").equals("0")&&rongyun.RetireOrCleanGroup(map.gets("id").toString(), map.gets("groupid").toString(), url)){
				model.setId(map.gets("id"));
				model.setGroupid(map.gets("groupid"));
				rongCloudeDao2.deletegroupone(model);
			
			}else if(rongyun.RetireOrCleanGroup(map.gets("id"), map.gets("groupid"), url1)){
				rongCloudeDao2.deletegroupall(map.gets("groupid"));
				rongCloudeDao2.deletegroupbygroupid(map.gets("groupid"));
			}
			
			sess.put(Tag_Tips.RESPCODE, "0");
		
		}catch (JsonException e){
			LogCvt.error(Tag_Tips.JSONERROR,e);
			throw new ServiceException("rongtoken业务流程出错");
		}
		catch (Exception e) {
			LogCvt.error(e.getMessage(),e);
			throw new ServiceException("rongtoken业务流程出错");
		}
		finally {
			map=null;
		}
		
	}


	@Override
	public void deleteGroupAll(Session sess) {
		// TODO Auto-generated method stub
		CMap map=null;
		try{
			map=SetToMap(sess);
			RongYun1 rongyun=new RongYun1();
			String url1="http://api.cn.ronghub.com/group/dismiss.json";
			if(rongyun.RetireOrCleanGroup(map.gets("id"), map.gets("groupid"), url1)){
				rongCloudeDao2.deletegroupall(map.gets("groupid"));
				rongCloudeDao2.deletegroupbygroupid(map.gets("groupid"));
				sess.put(Tag_Tips.RESPCODE, "0");
			}
			
		}
		catch (Exception e) {
			LogCvt.error(e.getMessage(),e);
			throw new ServiceException("rongtoken业务流程出错");
		}
		finally {
			map=null;
		}
		
	}

}
